package com.huowei.htzy.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.huowei.htzy.R;
import com.huowei.htzy.fragment.CartFragment;
import com.huowei.htzy.fragment.ClassFragment;
import com.huowei.htzy.fragment.HomeFragment;
import com.huowei.htzy.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class HomeActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @InjectView(R.id.h_frame_layout)
    FrameLayout hFrameLayout;
    @InjectView(R.id.h_radio_group)
    RadioGroup hRadioGroup;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.inject(this);
        //设置RadioGroup选择
        setRadioGroup();
        //初始化Fragment
        initFragment();

    }

    private void initFragment() {
        fragments.add(new HomeFragment());
        fragments.add(new ClassFragment());
        fragments.add(new CartFragment());
        fragments.add(new MineFragment());
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.h_frame_layout,fragments.get(0));
       transaction.commit();
        //想选中当前的RadioButton必须把它强转成RadioButton
        RadioButton rb = (RadioButton)hRadioGroup.getChildAt(0);
        rb.setChecked(true);

    }

    private void setRadioGroup() {
        hRadioGroup.setOnCheckedChangeListener(this);
    }

    /**
     * 监听当前点击的RadioButton
     * @param radioGroup
     * @param i
     */
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (i){
            case R.id.h_rb_home:
                transaction.replace(R.id.h_frame_layout,fragments.get(0));
                transaction.commit();
                break;
            case R.id.h_rb_class:
                transaction.replace(R.id.h_frame_layout,fragments.get(1));
                transaction.commit();
                Toast.makeText(this, "更多", Toast.LENGTH_SHORT).show();

                break;
            case R.id.h_rb_cart:
                transaction.replace(R.id.h_frame_layout,fragments.get(2));
                transaction.commit();
                Toast.makeText(this, "购物车", Toast.LENGTH_SHORT).show();

                break;
            case R.id.h_rb_mine:
                transaction.replace(R.id.h_frame_layout,fragments.get(3));
                transaction.commit();
                Toast.makeText(this, "我的", Toast.LENGTH_SHORT).show();
                break;

        }


    }
}
